var city = require('/assets/js/city.js');
Component({
  mixins: [],
  data: {
    cid:0,
    cityData: [],
    range: [0,0,0],
    oldcitys: []
  },
  props: {
    onGetCity: (data) => console.log(data),
    pid: 999,
    citys: []
  },
  didMount() {
    this.setData({
      cityData: city.cityData,
      oldcitys: this.propscitys
    })
    this.cityInit()
  },
  didUpdate() {
    // console.log("on");
    
    // if(JSON.stringify(this.data.oldcitys)!=JSON.stringify(this.props.citys)){
    //   this.cityInit()
    // }
  },
  didUnmount() {},
  methods: {
    test1(){
      console.log(this.data);
      console.log(this.props);
      // this.props.onGetid()
    },
    cityInit() {
      var p = 0, ci = 0, co = 0;
      if(this.props.citys.length<=0){
        this.setData({range:[0,0,0]})
      }else{
        if(this.props.citys[0]){
          for (const i in this.data.cityData) {
            if(this.data.cityData[i].value == this.props.citys[0]){
              p = +i;
              break;
            }
          }
          if(this.props.citys[1]){
            for (const j in this.data.cityData[p].children) {
              if(this.data.cityData[p].children[j].value == this.props.citys[1]){
                ci = +j;
                break;
              }
            }
            if(this.props.citys[2]){
            // console.log(3);
              for (const k in this.data.cityData[p].children[ci].children) {
                if(this.data.cityData[p].children[ci].children[k].value == this.props.citys[2]){
                  co = +k;
                  break;
                }
              }
            }
          }
        }
        this.setData({range:[p,ci,co]})
        console.log(this.data.range);
        
      }
    },
    cityChange(e) {
      // console.log(e);
      var index = e.detail.value
      if(this.data.range[0] != index[0]){
        this.setData({range:[index[0],0,0]})
      }else if(this.data.range[1] != index[1]){
        this.setData({range:[index[0],index[1],0]})
      }else{
        this.setData({range:index})
      }
      // this.setData({
      //   City: this.data.cityData[index[0]].children
      // })
      // this.setData({
      //   County: this.data.cityData[index[0]].children[index[1]].children
      // })
    },
    onGetCity(){
      console.log(this.data.range);
      var mainarr =[
        [+this.data.cityData[this.data.range[0]].value,this.data.cityData[this.data.range[0]].text],
        [+this.data.cityData[this.data.range[0]].children[this.data.range[1]].value,this.data.cityData[this.data.range[0]].children[this.data.range[1]].text],
        [+this.data.cityData[this.data.range[0]].children[this.data.range[1]].children[this.data.range[2]].value,this.data.cityData[this.data.range[0]].children[this.data.range[1]].children[this.data.range[2]].text]
      ]
      this.props.onGetCity(mainarr)
    }
  },
});
